package com.md.mod.controller;

import com.md.mod.bean.SysUser;
import com.md.mod.service.imp.IUserService;
import com.md.mod.utils.JSONResult;
import com.md.mod.utils.ReadProperties;
import org.n3r.idworker.Sid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * ********************************
 *
 * @author zhaoqingwei.
 * 天下英雄出我辈，一入江湖岁月催.
 * 20192019/1/2822:36
 * ********************************
 * 描述：
 * <p>
 * 注释：
 **/

@RestController
public class HelloController {
    private Logger log = LoggerFactory.getLogger(HelloController.class);
    @Autowired
    private ReadProperties readProperties;
    @Autowired
    private IUserService userService;
    @Autowired
    private Sid sid;

    @GetMapping("/re")
    public String getproperties(){
        ReadProperties re = new ReadProperties();
        BeanUtils.copyProperties(readProperties,re);
        return re.toString();
    }

    @PostMapping("/ts")
    public JSONResult test1(){
        int a = 0/1;
        int b = 1/0;
        return JSONResult.no();
    }

    @RequestMapping("/save")
    public String saveUser(SysUser u){
        try{
        SysUser user = new SysUser();
        log.debug("hello word!我进来了");
        user.setId(sid.nextShort());
        user.setUsername(u.getUsername());
        user.setPassword(u.getPassword());
        user.setNickname(u.getNickname());
        user.setIsDelete(1);
        user.setRegistTime(SimpleDateFormat.getDateInstance().parse("2019-01-29 12:33:40"));
        userService.saveUser(user);
        return user.toString();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping(value = "/users",method = RequestMethod.POST)
    public JSONResult selectUsersPage(SysUser user,@RequestParam("pagenum") int pagenum,@RequestParam("pagesize") int pagesize){

        return userService.selectUserListPage(user,pagenum,pagesize);
    }
}
